import numpy as np
import matplotlib.pyplot as plt
from scipy import misc # contains an image of a racoon!
from PIL import Image # for reading image files
my_array = np.array([1.1, 9.2, 8.1, 4.7])
my_array.shape
(4,)
my_array[2]
8.1
# Show dimensions og an array
my_array.ndim
1
array_2d = np.array([[1, 2, 3, 9],
[5, 6, 7, 8]])
print(f"array_2d has {array_2d.ndim} dimensions")
print(f"It's shape is {array_2d.shape}")
print(f"It has {array_2d.shape[0]} rows and {array_2d.shape[1]} columns")
print(array_2d)
array_2d has 2 dimensions It's shape is (2, 4) It has 2 rows and 4 columns [[1 2 3 9] [5 6 7 8]]
array_2d[1,2]
7
# To access an entire row, you can use the : operator
array_2d[0, :]
array([1, 2, 3, 9])
array_2d[:, 0]
array([1, 5])
Challenge:
18 in the last line of code.[97, 0, 27, 18][[ 0, 4], [ 7, 5], [ 5, 97]]Hint: You can use the : operator just as with Python Lists.
mystery_array = np.array([[[0, 1, 2, 3],
[4, 5, 6, 7]],
[[7, 86, 6, 98],
[5, 1, 0, 4]],
[[5, 36, 32, 48],
[97, 0, 27, 18]]])
# Note all the square brackets!
mystery_array.shape
(3, 2, 4)
mystery_array.ndim
3
# Axis 0: 3rd Element, Axis 1: 2nd Element, Axis 3: 4th Element
mystery_array[2, 1, 3]
18
mystery_array[2, 1, :]
array([97, 0, 27, 18])
mystery_array[:, :, 0]
array([[ 0, 4],
[ 7, 5],
[ 5, 97]])
a = np.arange(10, 30)
a
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29])
a to:¶aa containing all the values except for the first 12 (i.e., [22, 23, 24, 25, 26, 27, 28, 29])a_3 = a[-3:]
a_3
array([27, 28, 29])
a_4_5_6 = a[3:6]
a_4_5_6
array([13, 14, 15])
a_12_nex = a[12:]
a_12_nex
array([22, 23, 24, 25, 26, 27, 28, 29])
a_even = a[::2]
a_even
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28])
a, so that the first element comes last:¶[29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
12, 11, 10]
If you need a hint, you can check out this part of the NumPy beginner's guide
rev_a = np.flip(a)
rev_a
array([29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
12, 11, 10])
b = np.array([6,0,9,0,0,5,0])
nz_indices = np.nonzero(b)
nz_indices # This is a tuple
(array([0, 2, 5]),)
Hint: Use the .random() function
z = np.random.random((3,3,3))
z.shape
(3, 3, 3)
.linspace() to create a vector x of size 9 with values spaced out evenly between 0 to 100 (both included).¶x = np.linspace(0, 100, num=9)
x
array([ 0. , 12.5, 25. , 37.5, 50. , 62.5, 75. , 87.5, 100. ])
.linspace() to create another vector y of size 9 with values between -3 to 3 (both included). Then plot x and y on a line chart using Matplotlib.¶y = np.linspace(start=-3, stop=3, num=9)
plt.plot(x, y)
[<matplotlib.lines.Line2D at 0x7f98f08178e0>]
noise = np.random.random((128, 128, 3))
print(noise.shape)
plt.imshow(noise)
(128, 128, 3)
<matplotlib.image.AxesImage at 0x7f98f088abe0>
v1 = np.array([4, 5, 2, 7])
v2 = np.array([2, 1, 3, 3])
# These are treated like vectors, matrices and tensors
v1 + v2
array([ 6, 6, 5, 10])
# Python Lists vs ndarrays
list1 = [4, 5, 2, 7]
list2 = [2, 1, 3, 3]
list1 + list2
[4, 5, 2, 7, 2, 1, 3, 3]
v1 * v2
array([ 8, 5, 6, 21])
# list1 * list2 - Error
array_2d = np.array([[1,2,3,4],
[5,6,7,8]])
array_2d + 10
array([[11, 12, 13, 14],
[15, 16, 17, 18]])
array_2d * 5
array([[ 5, 10, 15, 20],
[25, 30, 35, 40]])

a1 = np.array([[1, 3],
[0, 1],
[6, 2],
[9, 7]])
b1 = np.array([[4, 1, 3],
[5, 8, 5]])
print(f'{a1.shape}: a has {a1.shape[0]} rows and {a1.shape[1]} columns.')
print(f'{b1.shape}: b has {b1.shape[0]} rows and {b1.shape[1]} columns.')
print('Dimensions of result: (4x2)*(2x3)=(4x3)')
(4, 2): a has 4 rows and 2 columns. (2, 3): b has 2 rows and 3 columns. Dimensions of result: (4x2)*(2x3)=(4x3)
Challenge: Let's multiply a1 with b1. Looking at the wikipedia example above, work out the values for c12 and c33 on paper. Then use the .matmul() function or the @ operator to check your work.
answ = np.matmul(a1, b1)
answ
array([[19, 25, 18],
[ 5, 8, 5],
[34, 22, 28],
[71, 65, 62]])
a1 @ b1
array([[19, 25, 18],
[ 5, 8, 5],
[34, 22, 28],
[71, 65, 62]])
img = misc.face()
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f99121adc10>
Challenge: What is the data type of img? Also, what is the shape of img and how many dimensions does it have? What is the resolution of the image?
type(img)
numpy.ndarray
img.ndim
3
img.shape
(768, 1024, 3)
Challenge: Convert the image to black and white. The values in our img range from 0 to 255.
grey_vals to convert the image to grey scale. .imshow() together with the colormap parameter set to gray cmap=gray to look at the results. grey_vals = np.array([0.2126, 0.7152, 0.0722])
sRGB_array = img/255
img_gray = sRGB_array @ grey_vals
# img_gray = np.matmul(sRGB_array, grey_vals)
plt.imshow(img_gray, cmap="gray")
<matplotlib.image.AxesImage at 0x7f9912463310>
Challenge: Can you manipulate the images by doing some operations on the underlying ndarrays? See if you can change the values in the ndarray so that:
1) You flip the grayscale image upside down
2) Rotate the colour image
3) Invert (i.e., solarize) the colour image. To do this you need to converting all the pixels to their "opposite" value, so black (0) becomes white (255).
img_gray_flipped = np.flip(img_gray)
plt.imshow(img_gray_flipped, cmap="gray")
<matplotlib.image.AxesImage at 0x7f98d020c790>
img_rot90 = np.rot90(img) # Transpose
plt.imshow(img_rot90)
<matplotlib.image.AxesImage at 0x7f98f0c35850>
solar_img = 255 - img
plt.imshow(solar_img)
<matplotlib.image.AxesImage at 0x7f9913389fa0>
file_name = 'yummy_macarons.jpg'
my_img = Image.open(file_name)
img_array = np.array(my_img)
plt.imshow(img_array)
<matplotlib.image.AxesImage at 0x7f99003559a0>
plt.imshow(255-img_array)
<matplotlib.image.AxesImage at 0x7f9900fd1e50>